/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.InterfacePropertyGenerator;
import com.inspur.edp.cef.api.attr.EnumDVAttribute;
import com.inspur.edp.cef.api.attr.EnumDVStrAttribute;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.designtime.api.element.GspEnumValue;
import com.inspur.edp.cef.entity.dependenceTemp.DataValidator;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.nestedConfigs.NestedObjectConfigGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.StringLiteral;

public class DataTypePropertyGenerator extends InterfacePropertyGenerator {
  private IGspCommonField field;
  private DataTypeDataGeneratorContext dataGenContext;

  public DataTypePropertyGenerator(
      IGspCommonField field, DataTypeDataGeneratorContext dataGenContext) {
    this.field = field;
    this.dataGenContext = dataGenContext;
  }

  @Override
  protected String getPropertyName() {
    DataValidator.checkForEmptyString(field.getLabelID(), "element.getLabelID()");
    return field.getLabelID();
  }

  @Override
  protected TypeInfo getPropertyType() {
    return field.getIsUdt() ? getUdtType() : getNonUdtType();
  }

  @Override
  protected String getFieldName() {
    return null;
  }

  private TypeInfo getUdtType() {
    CefGeneratorContext cefContext =
        dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext();
    return Utils.getUdtType(field.getUdtPkgName(), field.getUdtID(), cefContext);
  }

  private TypeInfo getNonUdtType() {
    switch (field.getObjectType()) {
      case None:
        return Utils.getNativeType(field,
            dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext());
      case Enum:
        String enumType =
                dataGenContext.getDataTypeGeneratorContext().getEnumTypes().get(field.getLabelID());
        return new TypeInfo(enumType);
      case Association:
        String assoType =
                dataGenContext.getDataTypeGeneratorContext().getAssociationTypes().get(field.getLabelID());
        return new TypeInfo(assoType);
      default:
        return null;
    }
  }


  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    ArrayList<AnnotationInfo> list = super.getGetterAttributeList();
    if (list == null) {
      list = new ArrayList<AnnotationInfo>();
    }
    if (field.getObjectType() == GspElementObjectType.Enum) {
      if(field.getEnumIndexType()== EnumIndexType.String){
        addEnumStrAttributeInfo(list);
      }
      else {
        addEnumAttributeInfo(list);
      }
    }
    if (field.getIsUdt() == false) {
      return list;
    }
    UnifiedDataTypeDef udtDef = null;
    if (field.getMappingRelation() == null) {
      throw new RuntimeException();
    }
//    addSimpleUdtAtrributeInfo(list, udtDef);
    return list;
  }

  private void addEnumAttributeInfo(ArrayList<AnnotationInfo> list) {
    int defaultValue = 0;

    for (GspEnumValue item : field.getContainEnumValues()) {
      GspEnumValue value = (GspEnumValue) item;
      if (value.getIsDefaultEnum()) {
        defaultValue = value.getIndex();
        break;
      }
    }
    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    //TODO: caf暂不支持, 主动加import
    dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().addImport(EnumDVAttribute.class.getName());
    TypeInfo typeInfo = new TypeInfo(EnumDVAttribute.class);
    annotationInfo.setTypeInfo(typeInfo);
    annotationInfo.setAnnotationType(AnnotationType.Normal);
    annotationInfo.getParams().put("defaultValue", ast.newNumberLiteral(String.valueOf(defaultValue)));
    list.add(annotationInfo);
  }
  private void addEnumStrAttributeInfo(ArrayList<AnnotationInfo> list) {
    String strDefaultValue = "";

    for (GspEnumValue item : field.getContainEnumValues()) {
      GspEnumValue value = (GspEnumValue) item;
      if (value.getIsDefaultEnum()) {
        strDefaultValue = value.getStringIndex();
        break;
      }
    }
    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    //TODO: caf暂不支持, 主动加import
    dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().addImport(EnumDVStrAttribute.class.getName());
    TypeInfo typeInfo = new TypeInfo(EnumDVStrAttribute.class);
    annotationInfo.setTypeInfo(typeInfo);
    annotationInfo.setAnnotationType(AnnotationType.Normal);
    StringLiteral stringLiteral=ast.newStringLiteral();
    stringLiteral.setLiteralValue(strDefaultValue);
    annotationInfo.getParams().put("strDefaultValue", stringLiteral);
    list.add(annotationInfo);
  }
//  private void addSimpleUdtAtrributeInfo(
//      ArrayList<AnnotationInfo> list, UnifiedDataTypeDef udt) {
//    MediateType configType = dataGenContext.getNestedConfigTypes().get(field.getLabelID());
//    DataValidator.checkForNullReference(configType, "configType");
//    AnnotationInfo info = new AnnotationInfo();
//    info.setAnnotationType(AnnotationType.SingleMember);
//    //TODO: caf暂不支持, 主动加import
//    dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().addImport(NestedConfigAtrribute.class.getName());
//    TypeInfo typeInfo = new TypeInfo(NestedConfigAtrribute.class);
//    info.setTypeInfo(typeInfo);
//    TypeLiteral param = ast.newTypeLiteral();
//    param.setType(ast.newSimpleType(ast.newSimpleName(configType.getClassName())));
////    info.setSingleParam(param);
//    info.getParams().put("configType",param);
//    list.add(info);
//  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {

    ArrayList<BaseGenerator> list = super.createChildGenerators();
    if (list == null) {
      list = new java.util.ArrayList<BaseGenerator>();
    }
    if (field.getIsUdt()) {
//      list.add(
//          new NestedObjectConfigGenerator(
//              dataGenContext.getDataTypeGeneratorContext().getDataType(), field, dataGenContext));
    }
    return list;
  }
}
